package com.jack.thinking.thread.core._4;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReentrantRead2Read {
	public static void main(String[] args) {
		Service service = new Service();
		new Thread() {
			public void run() {
				service.read();
			};
		}.start();
		
		new Thread() {
			public void run() {
				service.read();
			};
		}.start();
	}
}

class Service{
	private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
	
	public void read() {
		try {
			lock.readLock().lock();
			System.out.println("获得读锁: " + Thread.currentThread().getName() + " " + System.currentTimeMillis());
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.readLock().unlock();
		}
	}
}